//
//  StackedBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "StackedBarChartViewController.h"
@interface StackedBarChartViewController ()
@end

@implementation StackedBarChartViewController

BarChartView *sampleStackedBarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleStackedBarChartView=[[BarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleStackedBarChartView.backgroundColor = [UIColor whiteColor];
    sampleStackedBarChartView.delegate = self;
    [self.view addSubview:sampleStackedBarChartView];
    [self initChartStyleAndData];
}


-(void)initChartStyleAndData{
    [self setupBarLineChartView:sampleStackedBarChartView];
    [self setBarChartParam:sampleStackedBarChartView];
    [self setLegendStyle:sampleStackedBarChartView];
    [self initChartData];
    [sampleStackedBarChartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setupBarLineChartView:(BarChartView *)chartView
{
    chartView.descriptionText = @"descriptionText";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.maxVisibleValueCount = 60;
    chartView.pinchZoomEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
    chartView.drawBarShadowEnabled = NO;
    chartView.drawValueAboveBarEnabled = NO;
}

-(void)setBarChartParam:(BarChartView *)chartView{
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
    leftAxis.valueFormatter.maximumFractionDigits = 1;
    leftAxis.valueFormatter.negativeSuffix = @" $";
    leftAxis.valueFormatter.positiveSuffix = @" $";
    leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
    
    chartView.rightAxis.enabled = NO;
    
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionTop;
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    ChartLegend *l = chartView.legend;
    l.position = ChartLegendPositionBelowChartRight;
    l.form = ChartLegendFormSquare;
    l.formSize = 8.0;
    l.formToTextSpace = 4.0;
    l.xEntrySpace = 6.0;
}

- (void)initChartData
{
    [self setDataCount:11 range:100 chart:sampleStackedBarChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(BarChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val1 = (double) (arc4random_uniform(mult) + mult / 3);
        double val2 = (double) (arc4random_uniform(mult) + mult / 3);
        double val3 = (double) (arc4random_uniform(mult) + mult / 3);
        
        [yVals addObject:[[BarChartDataEntry alloc] initWithValues:@[@(val1), @(val2), @(val3)] xIndex:i]];
    }
    
    BarChartDataSet *set1 = nil;
    if (chartView.data.dataSetCount > 0)
    {
        set1 = (BarChartDataSet *)chartView.data.dataSets[0];
        set1.yVals = yVals;
        chartView.data.xValsObjc = xVals;
        [chartView notifyDataSetChanged];
    }
    else
    {
        set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"Statistics Vienna 2014"];
        set1.colors = @[ChartColorTemplates.vordiplom[0], ChartColorTemplates.vordiplom[1], ChartColorTemplates.vordiplom[2]];
        set1.stackLabels = @[@"Births", @"Divorces", @"Marriages"];
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 1;
        formatter.negativeSuffix = @" $";
        formatter.positiveSuffix = @" $";
        
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
        [data setValueFormatter:formatter];
        
        chartView.data = data;
    }
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected, stack-index %ld", (long)highlight.stackIndex);
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end